package com.letoken.platform.admin.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.admin.service.impl.StoreServiceImpl;
import com.letoken.platform.admin.util.CainiaoUtils;
import com.letoken.platform.pub.annotation.auth.Permission;
import com.letoken.platform.pub.annotation.pub.Trim;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.pub.req.PageData;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.repo.admin.res.menu.QueryAdminMenuRes;
import com.letoken.platform.repo.app.req.mall.StoreReq;
import com.letoken.platform.repo.app.res.mall.MallStoreRes;
import com.letoken.platform.repo.ecommerce.dao.EcommerceAfterOrderMapper;
import com.letoken.platform.repo.ecommerce.dao.EcommerceProductMapper;
import com.letoken.platform.repo.ecommerce.dao.EcommerceUserMasterOrderMapper;
import com.letoken.platform.repo.ecommerce.dao.EcommerceUserOrderMapper;
import com.letoken.platform.repo.ecommerce.dto.*;
import com.letoken.platform.repo.ecommerce.po.EcommerceAfterOrder;
import com.letoken.platform.repo.ecommerce.po.EcommerceProduct;
import com.letoken.platform.repo.ecommerce.po.EcommerceUserMasterOrder;
import com.letoken.platform.repo.mall.dao.AppMallInfoMapper;
import com.letoken.platform.repo.utlis.PageDataUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/mall/store")
@Api(tags = "商店管理")
public class StoreController {

    @Resource
    private AppMallInfoMapper appMallInfoMapper;

    @Resource
    private EcommerceProductMapper ecommerceProductMapper;

    @Resource
    private EcommerceUserOrderMapper ecommerceUserOrderMapper;

    @Resource
    private EcommerceAfterOrderMapper ecommerceAfterOrderMapper;

    @Resource
    private EcommerceUserMasterOrderMapper ecommerceUserMasterOrderMapper;

    @Resource
    private StoreServiceImpl service;


    @Autowired
    public void setRedisTemplate(StringRedisTemplate redisTemplate) {
        CainiaoUtils.setRedisTemplate(redisTemplate);
    }

    @Permission(uri = "/mall/store/query", qx = "/mall/store/query")
    @Trim
    @PostMapping("/query")
    @ApiOperation(value = "查询门店列表", notes = "查询门店列表")
    public ApiResponse<PageData<MallStoreRes>> list(@RequestBody StoreReq req) {

        return ApiResponse.create(PageDataUtil.getPageData(appMallInfoMapper.stores(new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize()), req)));
    }


    @Permission(uri = "/mall/store/info/summary", qx = "/mall/store/info/summary")
    @Trim
    @GetMapping("/info/summary")
    @ApiOperation(value = "门店信息汇总", notes = "门店信息汇总")
    public ApiResponse<PageData<MallStoreRes>> infoCount() {

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("storeTotalQuantity", appMallInfoMapper.count());
        hashMap.put("mallRevenue", ecommerceUserOrderMapper.incomeStatistics());
        hashMap.put("storeTodayQuantity", appMallInfoMapper.countToday());
        hashMap.put("mallTodayRevenue", ecommerceUserOrderMapper.todayIncomeStatistics());
        return ApiResponse.create(hashMap);
    }

    @Permission(uri = "/mall/store/products", qx = "/mall/store/products")
    @Trim
    @PostMapping("/products")
    @ApiOperation(value = "商品列表", notes = "商品列表")
    public ApiResponse<PageData<QueryAdminMenuRes>> products(@RequestBody ShopProductReq req) {
        return ApiResponse.create(PageDataUtil.getPageData(ecommerceProductMapper.products(new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize()), req)));
    }


    @Permission(uri = "/mall/store/product/update", qx = "/mall/store/product/update")
    @Trim
    @PostMapping("/product/update")
    @ApiOperation(value = "修改商品", notes = "修改商品")
    public ApiResponse storeProduct(@RequestBody ShopProductReq req) {
        EcommerceProduct ecommerceProduct = new EcommerceProduct();
        ecommerceProduct.setId(req.getId());
        ecommerceProduct.setPublishStatus(req.getPublishStatus());
        ecommerceProductMapper.updateById(ecommerceProduct);
        return ApiResponse.create();
    }


    @Permission(uri = "/mall/store/orders", qx = "/mall/store/orders")
    @Trim
    @PostMapping("/orders")
    @ApiOperation(value = "订单列表", notes = "订单列表")
    public ApiResponse orders(@RequestBody UserOrderReq req) {
        return ApiResponse.create(service.orders(req));
    }

    @Permission(uri = "/mall/store/orders/logistics", qx = "/mall/store/orders/logistics")
    @Trim
    @GetMapping("/orders/logistics")
    @ApiOperation(value = "订单物流信息", notes = "订单列表")
    public ApiResponse orderLogistics(@Param("id") Integer id, @Param("type") Integer type) {

        KuaidiReq kuaidiReq = new KuaidiReq();
        kuaidiReq.setIsRealTime(false);
        if (1 == type) {
            EcommerceUserMasterOrder masterOrder = ecommerceUserMasterOrderMapper.selectById(id);
            kuaidiReq.setNo(masterOrder.getDeliveryNo());
            try {
                kuaidiReq.setPhone(masterOrder.getReceiverPhone().trim().substring(7));
            } catch (Exception e) {
                log.error(e.toString());
                return ApiResponse.create(1111, "订单手机号码异常");
            }
        } else {
            EcommerceAfterOrder afterOrder = ecommerceAfterOrderMapper.selectById(id);
            kuaidiReq.setNo(afterOrder.getDeliveryNo());
            try {
                kuaidiReq.setPhone(afterOrder.getReceiverPhone().trim().substring(7));
            } catch (Exception e) {
                log.error(e.toString());
                return ApiResponse.create(1111, "订单手机号码异常");
            }
        }
        return ApiResponse.create(CainiaoUtils.query(kuaidiReq));
    }


//    @Permission(uri = "/mall/store/after_sale/orders/logistics", qx = "/mall/store/orders/logistics")
//    @Trim
//    @PostMapping("/after_sale/orders/logistics")
//    @ApiOperation(value = "售后订单物流信息", notes = "订单列表")
//    public ApiResponse afterSaleOrderLogistics(@RequestBody UserOrderConfirmReq req) {
//        return ApiResponse.create(service.afterSaleOrderLogistics(req.getId()));
//    }

    @Permission(uri = "/mall/store/order/summary", qx = "/mall/store/order/summary")
    @Trim
    @GetMapping("/order/summary")
    @ApiOperation(value = "订单信息汇总", notes = "订单信息汇总")
    public ApiResponse orderSummary() {
        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("orderQuantity", ecommerceUserOrderMapper.countQuantity());
        hashMap.put("mallRevenue", ecommerceUserOrderMapper.orderIncomeStatistics());
        hashMap.put("todayOrderQuantity", ecommerceUserOrderMapper.countTodayQuantity());
        hashMap.put("mallTodayRevenue", ecommerceUserOrderMapper.todayOrderIncomeStatistics());
        return ApiResponse.create(hashMap);
    }


    @Permission(uri = "/mall/store/orders/cancel", qx = "/mall/store/orders/cancel")
    @Trim
    @PostMapping("/orders/cancel")
    @ApiOperation(value = "订单取消", notes = "订单取消")
    public ApiResponse ordersCancel(@RequestBody UserOrderConfirmReq req) {
        try {
            service.cancelOrder(req);
        } catch (ApiServiceException e) {
            return ApiResponse.create(e.getCode(), e.getMessage());
        }
        return ApiResponse.create();
    }

    @Permission(uri = "/mall/store/orders/confirm_receipt", qx = "/mall/store/orders/confirm_receipt")
    @Trim
    @PostMapping("/orders/confirm_receipt")
    @ApiOperation(value = "订单确认收货", notes = "订单确认收货")
    public ApiResponse orderConfirmReceipt(@RequestBody UserOrderConfirmReq req) {
        try {
            service.orderConfirmReceipt(req);
        } catch (ApiServiceException e) {
            return ApiResponse.create(e.getCode(), e.getMessage());
        }
        return ApiResponse.create();
    }

    @Permission(uri = "/mall/store/after_sale/orders", qx = "/mall/store/after_sale/orders")
    @Trim
    @PostMapping("/after_sale/orders")
    @ApiOperation(value = "售后管理", notes = "售后管理")
    public ApiResponse<PageData> afterSaleOrders(@RequestBody ShopAfterOrderReq req) {
        return ApiResponse.create(PageDataUtil.getPageData(ecommerceAfterOrderMapper.afterSaleOrders(new Page<>(req.getPageReq().getPage(), req.getPageReq().getSize()), req)));
    }

    @Permission(uri = "/mall/store/after_sale/orders/audit", qx = "/mall/store/after_sale/orders/audit")
    @Trim
    @PostMapping("/after_sale/orders/audit")
    @ApiOperation(value = "售后订单审核", notes = "售后订单审核")
    public ApiResponse<PageData<QueryAdminMenuRes>> afterSaleOrders(@RequestBody ShopAfterOrderApproveReq req) {
        try {
            service.afterSaleOrdersAudit(req);
        } catch (ApiServiceException e) {
            return ApiResponse.create(e.getCode(), e.getMessage());
        }
        return ApiResponse.create();
    }
}
